//
//  GalleryMainCollectionCell.m
//  ArtDog
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GalleryMainCollectionCell.h"
#import "BaseCollectionView.h"
#import "StoreGoodsCollectionCell.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "ZWYNetTool.h"
#import "HLMacro.h"
#import "NSString+Common.h"
#import "UIView+Common.h"
#import "ExhibitTableViewCell.h"
#import "HomeExhibitWorkList.h"
#import "ExhibitWorkDetailsViewController.h"

@interface GalleryMainCollectionCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet BaseCollectionView *collectionView;
@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *loadArray;

@end

@implementation GalleryMainCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if (_loadArray == nil) {
        NSMutableArray *topicList = [NSMutableArray array];
        _loadArray = [NSMutableArray arrayWithObjects:topicList, topicList, nil];
    }
    [self setupBasic];
}

- (void)setupBasic
{
    
    [_collectionView registerNib:[UINib nibWithNibName:@"StoreGoodsCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"storeGoodsCollectionCell"];
}

- (void)setType:(NSInteger)type
{
    
    _type = type;
    [_collectionView reloadData];
    if (_collectionView.mj_header == nil) {
        [self refreshCollectionView];
    }
    if (_loadArray[type].count == 0) {
        [self handleData];
    }
    
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray[_type] = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            HomeExhibitWorkList *model = [HomeExhibitWorkList modelWithDic:dic];
            [_loadArray[_type] addObject:model];
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            HomeExhibitWorkList *model = [HomeExhibitWorkList modelWithDic:dic];
            [_loadArray[_type] addObject:model];
        }
        [_collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    
    urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v4/showExhibitWorks"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"sortType": [NSString stringWithFormat:@"%ld", _type + 1]}];
    
    return urlString;
}

#pragma mark - tableView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _loadArray[_type].count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoreGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"storeGoodsCollectionCell" forIndexPath:indexPath];
    cell.exhibitWorkModel = _loadArray[_type][indexPath.item];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ExhibitWorkDetailsViewController *detailsVC = [[ExhibitWorkDetailsViewController alloc]init];
    detailsVC.urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"exhibit/WorkDetailV4", [NSString stringWithFormat:@"%@", [_loadArray[_type][indexPath.item] idField]]] andParams:@{@"token": [NSString getUserDefaultToken], @"userId": [NSString getUserDefaultId], @"workID": [NSString stringWithFormat:@"%@", [_loadArray[_type][indexPath.item] idField]]}];
    UIViewController *vc = [self getVCRecently];
    [vc.navigationController pushViewController:detailsVC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 50) / 2, (SCREEN_WIDTH - 50) / 2 * 1.2 + 100);
}


#pragma mark - 刷新
- (void)refreshCollectionView
{
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_collectionView.mj_header mjRefreshStateHeaderStyle];
    
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray[_type].count % 10 == 0) {
            _pageIndex = _loadArray[_type].count / 10 + 1;
        } else {
            _pageIndex = _loadArray[_type].count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_collectionView.mj_footer mjRefreshAutoStateFooterStyle];
}

@end
